package 堆栈数组实现;

public class StackByArray {
    private int[] arr;
    private int top;

    public StackByArray(int size){
        arr=new int[size];
        top=-1;
    }
    //判断堆栈是否为空
    public boolean isEmpty(){
        if(top==-1){
            return true;
        }else{
            return false;
        }
    }
    //将指定数据压入堆栈
    public boolean push(int data){
        if(top>=arr.length){
            System.out.println("堆栈已满，无法再压入");
            return false;
        }else{
            arr[++top]=data;
            return true;
        }
    }
    //从堆栈弹出数据
    public int pop(){
        if(isEmpty()){
            return -1;
        }else{
            return arr[top--];
        }
    }
}
